package com.ryx.controller;

import com.ryx.aop.annotation.MyLog;
import com.ryx.constants.Constant;
import com.ryx.entity.SysUser;
import com.ryx.service.UserService;
import com.ryx.utils.DataResult;
import com.ryx.utils.JWTTokenUtil;
import com.ryx.vo.req.*;
import com.ryx.vo.resp.LoginRespVo;
import com.ryx.vo.resp.PageVo;
import com.ryx.vo.resp.UserOwnRoleRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @BelongsPackage: com.ryx.controller
 * @Author: 容永轩
 * @CreateTime: 2020-11-03
 * @Description:
 */
@RestController
@RequestMapping("/api")
@Api(tags = "组织管理-用户管理",description = "用户模块相关接口")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/user/login")
    @ApiModelProperty(value = "login接口")
    public DataResult<LoginRespVo> loginResp(@RequestBody @Valid LoginReqVo reqVo) {

        DataResult result = DataResult.success();
        result.setData(userService.login(reqVo));

        return result;
    }

    @PostMapping("/users")
    @ApiModelProperty(value = "分页用户接口")
    @MyLog(title = "组织管理-用户管理",action = "分页查询用户接口")
    @RequiresPermissions("sys:user:list")
//    public DataResult<PageInfo<SysUser>> pageInfoDataResult(@RequestBody UserPageReqVo vo){
    public DataResult<PageVo<SysUser>> pageInfoDataResult(@RequestBody UserPageReqVo vo) {


        DataResult result = DataResult.success();

        result.setData(userService.pageInfo(vo));

        return result;
    }

    @PostMapping("/user")
    @ApiModelProperty(value = "新增用户接口")
    @RequiresPermissions("sys:user:add")
    @MyLog(title = "组织管理-用户管理",action = "新增用户接口")
    public DataResult addUser(@RequestBody @Valid UserAddReqVo vo){
        userService.addUser(vo);
        return  DataResult.success();
    }



    @GetMapping("/user/logout")
    @ApiOperation(value = "用户退出登录")
    public DataResult logout(HttpServletRequest request){
        try {
            String accessToken=request.getHeader(Constant.ACCESS_TOKEN);
            String refreshToken=request.getHeader(Constant.REFRESH_TOKEN);
            userService.logout(accessToken,refreshToken);
        } catch (Exception e) {
            log.error("logout:{}",e.getMessage());
        }
        return DataResult.success();
    }


    @GetMapping("/user/roles/{userId}")
    @ApiModelProperty(value = "查询用户拥有的角色数据接口")
    @MyLog(title = "组织管理-用户管理",action = "查询用户拥有的角色数据接口")
    @RequiresPermissions("sys:user:role:update")
    public DataResult<UserOwnRoleRespVo> getUserOwnRole(@PathVariable String userId){

        DataResult<UserOwnRoleRespVo> result = DataResult.success();

        result.setData(userService.getUserOwnRole(userId));

        return result;
    }


    @PostMapping("/user/roles")
    @ApiOperation(value = "save user info interface")
    @MyLog(title = "组织管理-用户管理",action = "保存用户拥有的角色信息接口")
    @RequiresPermissions("sys:user:role:update")
    public DataResult saveUserInfo(@RequestBody @Valid UserOwnRoleReqVo vo){

        DataResult result =DataResult.success();

        userService.setUserOwnRole(vo);

        return result;
    }

    @GetMapping("/user/token")
    @ApiModelProperty(value = "用户刷新token")
    @MyLog(title = "组织管理-用户管理",action = "jwt token 刷新接口")
    public DataResult<String> refreshToken(HttpServletRequest request){
        String requestHeader = request.getHeader(Constant.REFRESH_TOKEN);

        DataResult<String> result = DataResult.success();

        result.setData(userService.refreshToken(requestHeader));

        return  result;
    }

    @PostMapping("/user/edit")
    @ApiModelProperty(value = "用户更新信息接口")
    @MyLog(title = "组织管理-用户管理",action = "列表修改用户信息接口")
    @RequiresPermissions("sys:user:update")
    public DataResult updateUserInfo(@RequestBody @Valid UserUpdateReqVo vo,HttpServletRequest request){
        DataResult result = DataResult.success();

        String userId = JWTTokenUtil.getUserId(request.getHeader(Constant.ACCESS_TOKEN));
        userService.updateUserInfo(vo,userId);

        return result;
    }

    @DeleteMapping("/user")
    @ApiModelProperty(value = "删除用户接口")
    @MyLog(title = "组织管理-用户管理",action = "批量/删除用户接口")
    @RequiresPermissions("sys:user:delete")
    public DataResult deleteUsers(@RequestBody @ApiParam(value = "用户id集合")List<String> userIds,HttpServletRequest request){

        String requestHeader = request.getHeader(Constant.ACCESS_TOKEN);
        String operationId = JWTTokenUtil.getUserId(requestHeader);
        userService.deleteUsers(userIds,operationId);

        return DataResult.success();
    }

    @GetMapping("/user/info")
    @ApiOperation(value = "用户信息详情接口")
    @MyLog(title = "组织管理-用户管理",action = "用户信息详情接口")
    public DataResult<SysUser> detailInfo(HttpServletRequest request){

        String accessToken = request.getHeader(Constant.ACCESS_TOKEN);
        String userId = JWTTokenUtil.getUserId(accessToken);
        DataResult<SysUser> result = DataResult.success();

        result.setData(userService.selectUserById(userId));

        return result;
    }

    @PutMapping("/user/info")
    @ApiOperation(value = "保存个人信息接口")
    @MyLog(title = "组织管理-用户管理",action = "保存个人信息接口")
    public DataResult userInfo(@RequestBody UserUpdateDetailInfoReqVo vo,HttpServletRequest request){

        DataResult result =DataResult.success();
        String accessToken = request.getHeader(Constant.ACCESS_TOKEN);
        String userId = JWTTokenUtil.getUserId(accessToken);

        userService.userUpdateDetailInfo(vo,userId);
        return result;
    }

    @PutMapping("/user/pwd")
    @ApiOperation(value = "修改用户密码接口")
    @MyLog(title = "组织管理-用户管理",action = "修改用户密码接口")
    public DataResult updateUserPassword(HttpServletRequest request,@RequestBody @Valid UserUpdatePwdReqVo vo){
        String accessToken = request.getHeader(Constant.ACCESS_TOKEN);
        String refreshToken = request.getHeader(Constant.REFRESH_TOKEN);

        userService.userUpdatePwd(vo,accessToken,refreshToken);

        return DataResult.success();
    }




}
